<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    .router-link-active{
      color:red;
    }
  </style>
</head>
<body>
  <div id="app">
   
    <h3>路由基础</h3>
    <router-link tag="button" to="/home">
      首页
    </router-link>
    <router-link tag="button" to="/news">
      新闻页
    </router-link>
    <router-link tag="button" to="/about">
      关于我们
    </router-link>
    <button @click="add_routes">动态添加一个路由</button>
    <button @click="$router.go(0)">刷新</button>
    <button @click="$router.go(-1)">回退一步</button>
    <router-link to="/list">点击跳转到动态添加的路由</router-link>
    <hr>
    <router-view></router-view>
    <!-- 在视图上 定义 路由组件出口 -->
    
  </div>
  <script src="./vue.js"></script>
  <script src="./vue-router.js"></script>
  <script>
    // 准备三个 路由组件的 options对象
    const Home = {
      template: `
        <div>
          <h2>我是首页</h2>   
        </div>
      `,
      mounted(){
        console.log(this.$router, 111);
      }
    }
    const News = {
      template: `
        <div>
          <h2>我是新闻页</h2>   
        </div>
      `,
      mounted(){
        console.log(this.$route);
      }
    }
    const About = {
      template: `
        <div>
          <h2>我是关于我们</h2>   
        </div>
      `
    }
    const List = {
      template: `
        <div>
          <h2>我是列表页</h2>   
        </div>
      `
    }
    // 404组件
    const NotFound = {
      template: `
        <div>
          <h2>404啦</h2>   
        </div>
      `
    }
    // 路由规则
    const routes = [
      {
        path: '/',
        redirect: '/home'
      },
      {
        path: '/home',
        component: Home
      },
      {
        path: '/news', 
        component: News
      },
      {
        path: '/about',
        component: About
      },
      {
        path: '*',
        component: NotFound
      }
    ]
    // 创建路由对象
    const router = new VueRouter({
      routes // 代表路由规则 数组
    })
    // console.log(router, 222);
    const vm = new Vue({
      el: '#app',
      router,
      methods: {
        add_routes(){
          this.$router.addRoutes([
            {
              path: '/list',
              component: List
            }
          ])
        }
      }
    })
  </script>
</body>
</html>